<?php

/*
  Project: Blogmer - Open Source Blogging Software
  Type: PHP
  Desc: Contains all settings of blog.
  Author: Yashpal
  Usage:  	$this->load->model('Settings_model','',TRUE);
			$this->Settings_model->Blog_Settings();  - get settings value from database
			

  Function name : 
  Blog_Settings   -> This function will give the blog settings values.
 

*/


class Main_model extends Model 
{
    function Main_model()
    { parent::Model();}
	
	
	//load posts content based on id .We'll create array for post content
	//Name it as blogmer
		 
	function SinglePostView($id,$full=false,$title="",$checktitle=false)
	{
		$blogmer['error']= FALSE;
		//check for id v/s title
		if($checktitle)
		  $this->db->where(array('URLTitle'=>$title));
		else
		  $this->db->where(array('PostID'=>$id));
		  
		$query = $this->db->get('posts');
		if($query->num_rows()<=0)
		{
			$blogmer['error']= TRUE;
			$blogmer['description']="<h2>404 Error</h2>";
			return $blogmer;
		}
		
		
		$posts = $query->row_array();
		$this->session->set_userdata(array($title=>$posts['Comments']));	
			
		$content= $posts['Content'];
		
				
		//giving such format to date "Thursday, February 6th, 2008 at 2:11 PM" help by xwero
			
		$time=explode(":",substr($posts['PostedDate'],11,8));
		if($time['0']>12)
			$finaltime=($time['0']-12).":".$time['1']." PM";
		else
			$finaltime=$time['0'].":".$time['1']." AM";	
					
		//Check for <|more|> and get content
		//checking whether we want to show full content or short
		if ($full==false)
		{
			//we are showing short content
			$replace=anchor('blog/post/'.url_title($posts['Title']),'<span class="read-more">Read Complete Story</span>');
			$blogmer['content'] = preg_replace('/\|\|more\|\|.+/s',$replace,$content); 
					
		}
		else
		{
			//we want full content preview excluding <|more|> 
			$blogmer['content'] = preg_replace('/\|\|more\|\|/s','',$content);
		}
			
		$arraycats = explode(',',$posts['CatID']);
		if(count($arraycats)>1)
		{				 
			//$cats="";
			for($i=0;$i<count($arraycats);$i++)
			{
				$t=self::getcatstringfromid($arraycats[$i]);
				$mycats[]= anchor('blog/category/'.$t,$t);
			}
				
			$blogmer['categories']=implode(' , ',$mycats);
			
		}
		else
		{
			$t=self::getcatstringfromid($posts['CatID']);
			$blogmer['categories']= anchor('blog/category/'.$t,$t);
		}
				
			
		$blogmer['date']= date("l, F jS, Y ",strtotime($posts['PostedDate']))." at ".$finaltime;
		$blogmer['post_title']=$posts['Title'];
		$blogmer['post_status']=$posts['Status'];
		$blogmer['author_name']=$posts['Author'];
		$blogmer['title_link']=anchor('blog/post/'.url_title($posts['Title'],'dash'),$posts['Title']);
		$blogmer['comment_link']=anchor('blog/post/'.url_title($posts['Title'].'#comments','dash'),$posts['TotalComments']);
		$blogmer['comment_no']=$posts['TotalComments'];
		$blogmer['total_comments']=$posts['TotalComments'];
		$blogmer['full']=$full;
		
		
				
	return $blogmer;
	}
	
	//generate top page links.
	function PageLinks()
	{  
		$strr="";
	  	$query = $this->db->get('pages');
		foreach ($query->result() as $row)
	 	{
			if($row->Status==1)
			$strr.="<li>".anchor('blog/page/'.$row->URLTitle,$row->Label)."</li>";
		}	
		return $strr;
	}
	
	function SinglePage($URLTitle)
	{
	  	$blogmer['error']= FALSE;
		$this->db->where(array('URLTitle'=>$URLTitle));
		$query = $this->db->get('pages');
		if($query->num_rows()<=0)
		{
 		   $blogmer['error']= TRUE;
			$blogmer['description']="<h2>404 Error</h2>";
			
		}
		else
		{
			$posts = $query->row_array(0);  //returning 2d array so we want only 1d array.so get that...hehehe
			$blogmer['date']= "";
			$blogmer['post_title']=$posts['Label'];
			$blogmer['author_name']="";
			$blogmer['title_link']=anchor('blog/page/'.$posts['URLTitle'],$posts['Label']);
			$blogmer['comment_link']="";
			$blogmer['total_comments']="";
			$blogmer['full']=FALSE;
			$blogmer['content']=$posts['Content'];
		}
		return $blogmer;
		 
	}	
	
	
	function getcatstringfromid($catid)
	{
			$temp="";
			$this->db->where('CatID', $catid); 
			$query = $this->db->get('categories'); 
			foreach($query->result() as $row)
			   $temp = $row->Cat;
			return $temp;
						
	}
	function getcatidfromstring($cat)
	{
			$temp="";
			$this->db->where('Cat', $cat); 
			$query = $this->db->get('categories'); 
			foreach($query->result() as $row)
			   $temp = $row->CatID;
			return $temp;			
	}
	
	function categories($cat)
	{
		
		$catid=self::getcatidfromstring($cat);
		$this->db->like('CatID', $catid); 
		$this->db->from('posts');
	 	$cnt=$this->db->count_all_results();
		
		$config['base_url'] = site_url().'/blog/category/'.$cat;
		$config['total_rows'] =$cnt;
		$config['per_page'] = '10';
		$config['num_links'] = '3';
		$config['uri_segment'] = '4';

		$this->pagination->initialize($config); 
		$this->db->like('CatID', $catid); 
		$this->db->order_by("PostID", "desc"); 
		$query = $this->db->get('posts',$config['per_page'],$this->uri->segment(4));
		if($query->num_rows()<=0)
		  	  return 'There are no posts to show';
		
		$strr=array();
		$i=0;
		foreach ($query->result() as $row)
		{
			$dt= self::SinglePostView($row->PostID,false);
			
			if($dt['post_status']==1)
			{ $strr[$i]=$dt; 	 $i++; }
			
		}
		$strr['last']='<span class="nav-link">'.$this->pagination->create_links().'</span>';
		return $strr;
		 
	}
	
	function archives($date,$id)
	{
		
		$dates=explode("-",$date);
		$this->db->like('PostedDate',$dates[0]."-".$dates[1], 'after');
		$this->db->from('posts');
	 	$cnt=$this->db->count_all_results();
		
		$config['base_url'] = site_url().'/blog/archives/'.$date.'/pages/';
		$config['total_rows'] =$cnt;
		$config['per_page'] = '10';
		$config['num_links'] = '3';
		$config['uri_segment'] = '5';


		$this->pagination->initialize($config); 
		$this->db->like('PostedDate',$dates[0]."-".$dates[1], 'after');
		$this->db->order_by("PostID", "desc"); 
		$query = $this->db->get('posts',$config['per_page'],$this->uri->segment(5));
		if($query->num_rows()<=0)
		  	  return 'There are no posts to show';
		  
		$strr=array();
		$i=0;
		foreach ($query->result() as $row)
		{
			$dt= self::SinglePostView($row->PostID,false);
			
			if($dt['post_status']==1)
			{ $strr[$i]=$dt; 	 $i++; }
		}
		$strr['last']= '<span class="nav-link">'.$this->pagination->create_links().'</span>';
		return $strr;
		 
	}
	
	function home($posts)
	{
		$this->db->from('posts');
	 	$cnt=$this->db->count_all_results();
		
		$config['base_url'] = site_url().'/blog/index/pages/';
		$config['total_rows'] =$cnt;
		$config['per_page'] = $posts;
		$config['num_links'] = '3';
		$config['uri_segment'] = '4';

		$this->pagination->initialize($config); 
		$this->db->order_by("PostID", "desc"); 
		$query = $this->db->get('posts',$posts,$this->uri->segment(4));
		if($query->num_rows()<=0)
		{
			  return 'There are no posts to show';
		}
		//$strr=array();
		$i=0;
		foreach ($query->result() as $row)
		{		
			$dt= self::SinglePostView($row->PostID,false);
			
			if($dt['post_status']==1)
			{ $strr[$i]=$dt; 	 $i++; }
			
		}
		$strr['last']= '<span class="nav-link">'.$this->pagination->create_links().'</span>';
		return $strr;
		 
	}
	
	
	//function gives latest posts for feed
	function get_recent_posts($number=10)
	{
        $this->db->limit($number);
		$this->db->orderby('PostedDate', 'desc');
        
        return $this->db->get('posts');
	} //end of function

	
	

}// end of class
		
?>